import { message, notification } from 'antd';

type ToastType = 'success' | 'error' | 'loading' | 'info';

interface ToastOptions {
  content: string;
  duration?: number; // 秒，默认 2
  key?: string; // 用于更新同一个提示
}

class MessageTool {
  /**
   * 成功提示
   */
  success({ content, duration = 2, key }: ToastOptions) {
    message.success({ content, duration, key });
  }

  /**
   * 失败提示
   */
  error({ content, duration = 2, key }: ToastOptions) {
    message.error({ content, duration, key });
  }

  /**
   * 普通提示
   */
  info({ content, duration = 2, key }: ToastOptions) {
    message.info({ content, duration, key });
  }

  /**
   * 加载提示，返回关闭函数
   */
  loading({ content, duration = 0, key }: ToastOptions) {
    const loadingKey = key || 'loading';
    message.loading({ content, key: loadingKey, duration }); // duration=0 永不自动关闭
    return () => message.destroy(loadingKey); // 返回手动关闭函数
  }

  /**
   * 通知栏形式的提示
   */
  notify(type: ToastType, title: string, description?: string) {
    notification[type]({
      message: title,
      description,
      duration: 3,
      placement: 'topRight',
    });
  }
}

export default new MessageTool();
